﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class manager_edit : System.Web.UI.Page
{
    Model.Manager manager = new Model.Manager();
    BLL.Business.ManagerBLL managerBLL = new BLL.Business.ManagerBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)// 数据绑定
        {
            DataDindGrid();
        }
            
    }
    protected void ManagerList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int managerId = Int32.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "updateManager") // 编辑
            {
                this.panel1.Visible = true;
                DataSet ds = new DataSet();
                ds = managerBLL.SelectByID(managerId);
                if ((ds == null) ||
                 (ds.Tables.Count == 0) ||
                 (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                {
                }
                else
                {
                    this.name.Text = ds.Tables[0].Rows[0]["Manager_Namestr"].ToString();
                    this.password.Text = ds.Tables[0].Rows[0]["Manager_Passwordstr"].ToString();
                }
            }
            else// 删除
            {
                if (managerBLL.DeleteManager(managerId))
                { DataDindGrid(); }
                else
                {
                    Response.Write("<script>alter('出现错误，请检查！')</script>");
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }

    public void DataDindGrid()
    {
        try
        {
            DataSet ds = new DataSet();
            ds = managerBLL.SelectAll();
            if ((ds == null) ||
                  (ds.Tables.Count == 0) ||
                  (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            {
            }
            else
            {
                ManagerList.DataSource = ds;
                ManagerList.DataBind();
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 确定修改
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        try
        {
            String name = this.name.Text.ToString();
            String password = this.password.Text.ToString();
            manager.Manager_Namestr = name;
            manager.Manager_Passwordstr = password;

            if (managerBLL.UpdateManager(manager))
            {
                DataDindGrid();
            }
            else
            {
                Response.Write("<script>alter('出现错误，请检查！')</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
}
